<?php

/*
 * 订单管理
 */

namespace app\admin\controller\order;

use app\admin\Controller;
use think\Db;
use think\Exception;
use think\Request;

class Order extends Controller
{
    use \traits\controller\Jump;
    
    public function index()
    {
        $model_order = model('Order');
        $condition = array();
        if ($_GET['order_sn']) {
            $condition['order_sn'] = $_GET['order_sn'];
        }
        if ($_GET['store_name']) {
            $condition['store_name'] = $_GET['store_name'];
        }
        if (in_array($_GET['order_state'], array('0', '10', '15', '20', '30', '40'))) {
            $condition['order_state'] = $_GET['order_state'];
        }
        if ($_GET['payment_code']) {
            $condition['payment_code'] = $_GET['payment_code'];
        }
        if ($_GET['buyer_name']) {
            $condition['buyer_name'] = $_GET['buyer_name'];
        }
        
        $query_date = $this->query_date();
        if ($query_date && is_array($query_date)) {
            $condition['add_time'] = $query_date;
        }
        
        $pageConfig = [
            'path' => url('admin/order.order/index'),
            'query' => $this->request->param(),
            'page' => input('curpage/d') ? : 1,
            'type' => '\\Page',
            'var_page' => 'curpage'
        ];
        $order_list = $model_order->getOrderList($condition, 20, '*', 'order_id desc', $pageConfig);
        if (isset($order_list['page'])) {
            $this->view->assign('show_page', $order_list['page']);
        }
        $order_list = $order_list['list'] ? : [];

        foreach ($order_list as $order_id => $order_info) {
            //显示取消订单
            $order_list[$order_id]['if_cancel'] = $model_order->getOrderOperateState('system_cancel', $order_info);
            //显示收到货款
            $order_list[$order_id]['if_system_receive_pay'] = $model_order->getOrderOperateState('system_receive_pay', $order_info);
        }
        
        //显示支付接口列表(搜索)
        $payment_list = model('Common/Payment')->getPaymentOpenList();
        $this->view->assign('payment_list', $payment_list);

        $this->view->assign('order_list', $order_list);
        //$this->view->assign('show_page', $model_order->showpage());
        return $this->view->fetch();
    }

    /**
     * 平台订单状态操作
     *
     */
    public function change_state()
    {
        $order_id = input('order_id/d');
        if ($order_id <= 0) {
            $this->error('订单不存在', $_POST['ref_url']);
        }
        $model_order = model('Common/Order');

        //获取订单详细
        $condition = array();
        $condition['order_id'] = $order_id;
        $order_info = $model_order->getOrderInfo($condition);

        if ($_GET['state_type'] == 'cancel') {
            $result = $this->_order_cancel($order_info);
        } elseif ($_GET['state_type'] == 'receive_pay') {
            $result = $this->_order_receive_pay($order_info, input('post.'));
        } elseif ($_GET['state_type'] == 'offline_receive_pay') {
            $result = $this->_order_offline_receive_pay($order_info, input('post.'));
        }
        
        if (!is_array($result)) {
            return $result;
        }
        
        if (!$result['status']) {
            return $this->error($result['msg']);
        } else {
            return $this->success('操作成功', $result['url']);
        }
    }

    /**
     * 系统取消订单
     */
    private function _order_cancel($order_info)
    {
        $order_id = $order_info['order_id'];
        $model_order = model('Common/Order');
        $logic_order = model('Common/Order', 'logic');
        $if_allow = $model_order->getOrderOperateState('system_cancel', $order_info);
        if (!$if_allow) {
            return callback(false, '无权操作');
        }
        $result = $logic_order->changeOrderStateCancel($order_info, 'system', $this->admin_info['name']);
        if ($result['status']) {
            //$this->log('取消订单,订单号:' . $order_info['order_sn'], 1);
        }
        return $result;
    }

    /**
     * 系统确认凭证，收到线下付款
     */
    public function _order_offline_receive_pay($order_info, $post)
    {
        $order_id = $order_info['order_id'];
        $pay_sn = $order_info['pay_sn'];
        $model_order = model('Common/Order');
        $logic_order = model('Common/Order', 'logic');
        $order_pay = $model_order->getOrderPayInfo(array('pay_sn' => $pay_sn));
        if (!$this->request->isPost()) {
            $this->view->assign('order_info', $order_info);
            $this->view->assign('order_pay', $order_pay);
            return $this->view->fetch('offline_receive_pay');
        }
        if ($post['is_pass']) {
            //通过凭证，将order_state改成20,并将时间改为选择时间
            $order_data['order_state'] = ORDER_STATE_PAY;
            $order_data['payment_time'] = strtotime($post['payment_time']);
        } else {
            //凭证不通过,将order_state改成10并系统通知用户
            $order_data['order_state'] = ORDER_STATE_NEW;
            //发送用户站内信
            $msg = '您的订单凭证: ' . $order_pay['order_cr'] . ' 模糊不清或内容有误，请重新提交订单凭证或联系商家。';
            $model_msg = model('Common/Message');
            $buyer_id = $order_info['buyer_id'];
            $msg_param = array(
                'from_member_name' => $order_info['store_name'],
                'member_id' => $order_info['buyer_id'],
                'to_member_name' => $order_info['buyer_name'],
                'msg_content' => $msg,
                'message_type' => 1
            );
            $model_msg->saveMessage($msg_param);
        }
        $result = $model_order->editOrder($order_data, array('order_id' => $order_id));
        return ['status' => $result, 'url' => 'admin/order.order/index'];
    }

    /**
     * 系统收到货款
     * @throws Exception
     */
    private function _order_receive_pay($order_info, $post)
    {
        $order_id = $order_info['order_id'];
        $model_order = model('Common/Order');
        $logic_order = model('Common/Order', 'logic');
        $if_allow = $model_order->getOrderOperateState('system_receive_pay', $order_info);
        if (!$if_allow) {
            return callback(false, '无权操作');
        }

        if (!$this->request->isPost()) {
            $this->view->assign('order_info', $order_info);
            //显示支付接口列表
            $payment_list = model('Common/Payment')->getPaymentOpenList();
            //去掉预存款和线下支付
            foreach ($payment_list as $key => $value) {
                if ($value['payment_code'] == 'predeposit' || $value['payment_code'] == 'offline') {
                    unset($payment_list[$key]);
                }
            }
            $this->view->assign('payment_list', $payment_list);
            return $this->view->fetch('receive_pay');
        }
        $order_list = $model_order->getOrderList(array('pay_sn' => $order_info['pay_sn'], 'order_state' => ORDER_STATE_NEW));
        $result = $logic_order->changeOrderReceivePay($order_list, 'system', $this->admin_info['name'], $post);
        if ($result['status']) {
            //$this->log('将订单改为已收款状态,订单号:' . $order_info['order_sn'], 1);
        }
        $result['url'] = 'admin/order.order/index';
        return $result;
    }

    /**
     * 查看订单
     *
     */
    public function show()
    {
        $order_id = intval($_GET['order_id']);
        if ($order_id <= 0) {
            $this->error('订单不存在');
        }
        $model_order = model('Common/Order');
        $order_info = $model_order->getOrderInfo(array('order_id' => $order_id), array('order_goods', 'order_common', 'store'));

        //订单变更日志
        $log_list = $model_order->getOrderLogList(array('order_id' => $order_info['order_id']));
        $this->view->assign('order_log', $log_list);

        //退款退货信息
        $model_refund = model('Common/RefundReturn');
        $condition = array();
        $condition['order_id'] = $order_info['order_id'];
        $condition['seller_state'] = 2;
        $condition['admin_time'] = array('gt', 0);
        $return_list = $model_refund->getReturnList($condition);
        $this->view->assign('return_list', $return_list);

        //退款信息
        $refund_list = $model_refund->getRefundList($condition);
        $this->view->assign('refund_list', $refund_list);

        //卖家发货信息
        if (!empty($order_info['extend_order_common']['daddress_id'])) {
            $daddress_info = model('Common/Daddress')->getAddressInfo(array('address_id' => $order_info['extend_order_common']['daddress_id']));
            $this->view->assign('daddress_info', $daddress_info);
        }

        $this->view->assign('order_info', $order_info);
        return $this->view->fetch();
    }

}
